//
//  ZGLPlayLocationVC.m
//  Faker
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "ZGLPlayLocationVC.h"
#import "ZGLPlayFirstView.h"
#import "ZGLPlayFirstModel.h"
#import <UIImageView+AFNetworking.h>
#import <SDWebImage/UIButton+WebCache.h>
#import "ZGLCityListVC.h"
#import "ZGLHTTPTOOL.h"
#import "ZGLPlayHotButtonVC.h"
#import "ZGLPlayDetailModel.h"
#import "ZGLPlayMapVC.h"
#import <AMapSearchKit/AMapSearchKit.h>
#import <MAMapKit/MAMapKit.h>


#pragma mark 1 - 声明属性 签协议
@interface ZGLPlayLocationVC ()<ZGLCityListVCDelegate, MAMapViewDelegate, AMapSearchDelegate>
@property (nonatomic, strong) UIButton *button;
@property (nonatomic, strong) NSString *cityName;
@property (nonatomic, strong) NSArray *cityArr;
@property (nonatomic, strong) NSMutableArray *cityModelArr;
@property (nonatomic, strong) NSMutableArray *nextModelArr;
@property (nonatomic, strong) MAMapView *mapView;
@property (nonatomic, strong) AMapSearchAPI *search;
@property (nonatomic, strong) AMapGeoPoint *location;

@end

@implementation ZGLPlayLocationVC

-(void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
}



- (instancetype)init
{
    self = [super init];
    if (self) {
        [self creatLoactionButton];
    }
    return self;
}
-(void)creatMapViewWithCityName:(NSString *)address {
    /* 创建对象 */
    self.mapView = [[MAMapView alloc] init];
    [self.view addSubview:self.mapView];
    self.mapView.delegate = self;
    self.search = [[AMapSearchAPI alloc] init];
    self.search.delegate = self;
    //正向地理编码
    AMapGeocodeSearchRequest *geoRequest = [[AMapGeocodeSearchRequest alloc] init];
    geoRequest.address = address;
    //地址编码查询接口
    [self.search AMapGeocodeSearch:geoRequest];
    
}

#pragma mark - 实现正向地理编码的回调函数
- (void)onGeocodeSearchDone:(AMapGeocodeSearchRequest *)request response:(AMapGeocodeSearchResponse *)response {
    if(response.geocodes.count == 0) {
        return;
    }
    //处理搜索结果
    for (AMapGeocode *p in response.geocodes) {
        self.location = p.location;
    }
}



#pragma mark 2 - 主程序入口
- (void)viewDidLoad {
    [super viewDidLoad];
    /* 特殊热门城市数组 */
    self.cityArr = @[@"北京", @"上海", @"广州", @"深圳", @"成都", @"重庆", @"杭州", @"武汉", @"南京", @"天津"];
    /* bar设计 */
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"zaihufanhui"] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarButtonItem)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"查看地图" style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonItem)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
    /* 添加背景毛玻璃 */
    [self creatBackImage];
    /* 设置key值*/
    [MAMapServices sharedServices].apiKey = @"f47e32d3102eda1c75f67e13afe18c11";
    /* POI搜索 */
    /* 初始化 */
    [AMapSearchServices sharedServices].apiKey = @"f47e32d3102eda1c75f67e13afe18c11";
    // Do any additional setup after loading the view.
}
-(void)viewWillDisappear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
}
#pragma mark 2 ** 左边bar点击事件
-(void)leftBarButtonItem {
    /* 获取button上面的值 */
    NSString *str = [self.button.titleLabel.text substringFromIndex:3];
    /* 通过协议传到另一个界面 */
    [self.delegate passCityName:str];
    [self.navigationController popToRootViewControllerAnimated:YES];
}
-(void)rightBarButtonItem {
    NSString *str = [self.button.titleLabel.text substringFromIndex:3];
    ZGLPlayMapVC *mapVC = [[ZGLPlayMapVC alloc] init];
    mapVC.location = self.location;
    mapVC.city = str;
    [self.navigationController pushViewController:mapVC animated:YES];
    
}
#pragma mark 3 - 属性set方法重写
/* 定位城市名字属性 */
-(void)setCity:(NSString *)city {
    if (_city != city) {
        _city = city;
    }
    if ([self.cityArr containsObject:city]) {
        [self buttonHandleData:city];
        self.button.frame = CGRectMake(40 * ScaleX, 300 * ScaleX, 295 * ScaleX, 40 * ScaleX);
    }
    [self creatMapViewWithCityName:city];
    [self.button setTitle:[NSString stringWithFormat:@"定位：%@", self.city] forState:UIControlStateNormal];
    [self.view reloadInputViews];
}
/* 装model的数组 用来显示六个button */
-(void)setModelArr:(NSMutableArray *)modelArr {
    if (_modelArr != modelArr) {
        _modelArr = modelArr;
    }
    
    [self.view reloadInputViews];
}
#pragma mark 4 - 设置背景照片
-(void)creatBackImage {
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, Width, Height)];
    [self.view addSubview:imageView];
    imageView.image = [UIImage imageNamed:@"playDingWei.jpg"];
    /* 添加毛玻璃效果 */
   UIVisualEffectView *visual = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
    visual.frame = imageView.frame;
    visual.alpha = 0.8f;
    [imageView addSubview:visual];
}
#pragma mark 5 - 创建六个button
-(void)creatButtonView {
    [self.view reloadInputViews];
    for (int i = 0; i < 2; i++) {
    for (int j = 0; j < 3; j++) {
        ZGLPlayFirstView *view = [[ZGLPlayFirstView alloc] initWithFrame:CGRectMake(0 + 125 * ScaleX * j, (80 + 100 * i) * ScaleX, 125 * ScaleX, 100 * ScaleX)];
        [self.view addSubview:view];
        view.button.frame = CGRectMake(40 * ScaleX, 25 * ScaleX, 45 * ScaleX, 45 * ScaleX);
        view.button.tag = j + 3 * i + 1;
        ZGLPlayFirstModel *model = [self.cityModelArr objectAtIndex:j + 3 * i];
        [view.button sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", model.icon]] forState:UIControlStateNormal];
        [view.button addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside];
        view.label.text = [NSString stringWithFormat:@"%@", model.category];
        view.label.frame = CGRectMake(45 * ScaleX, 70 * ScaleX, 40 * ScaleX, 20 * ScaleX);
    }
    }
}
#pragma mark 5 ** 六个button点击事件
-(void)button:(UIButton *)button {
    ZGLPlayFirstModel *model = [self.cityModelArr objectAtIndex:button.tag - 1];
    NSString *str = [self.button.titleLabel.text substringFromIndex:3];
    if (button.tag == 1) {
        [self.delegate passCityName:str];
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
    else if (button.tag == 2) {
        ZGLPlayHotButtonVC *buttonVC = [[ZGLPlayHotButtonVC alloc] init];
        buttonVC.cityName = str;
        NSMutableString *urlStr = [NSMutableString stringWithString: @"http://wl.myzaker.com/?_appid=AndroidPhone&_v=6.4.1&_version=6.41&c=activity_list&category=1&city="];
        [urlStr appendString:str];
        NSString *striEncoding = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        [ZGLHTTPTOOL GETWithURL:striEncoding withBody:nil withHttpHead:nil responseStyle:JSON withSuccess:^(id result) {
            self.nextModelArr = [NSMutableArray arrayWithCapacity:0];
            NSDictionary *dic = [result objectForKey:@"data"];
            NSArray *arr = [dic objectForKey:@"weekends"];
            for (NSDictionary *dic in arr) {
                ZGLPlayDetailModel *model = [ZGLPlayDetailModel modWithDic:dic];
                [self.nextModelArr addObject:model];
               
            }
             buttonVC.modelArr = self.nextModelArr;
        } withFail:^(id result) {
            
        }];
        buttonVC.itemName = model.category;
        [self.navigationController pushViewController:buttonVC animated:YES];
    }
    else if (button.tag == 3) {
        ZGLPlayHotButtonVC *buttonVC = [[ZGLPlayHotButtonVC alloc] init];
        buttonVC.cityName = str;
        NSMutableString *urlStr = [NSMutableString stringWithString: @"http://wl.myzaker.com/?_appid=AndroidPhone&_v=6.4.1&_version=6.41&c=activity_list&category=4&city="];
        [urlStr appendString:str];
        NSString *striEncoding = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        [ZGLHTTPTOOL GETWithURL:striEncoding withBody:nil withHttpHead:nil responseStyle:JSON withSuccess:^(id result) {
            self.nextModelArr = [NSMutableArray arrayWithCapacity:0];
            NSDictionary *dic = [result objectForKey:@"data"];
            NSArray *arr = [dic objectForKey:@"weekends"];
            for (NSDictionary *dic in arr) {
                ZGLPlayDetailModel *model = [ZGLPlayDetailModel modWithDic:dic];
                [self.nextModelArr addObject:model];
                buttonVC.modelArr = self.nextModelArr;
            }
        } withFail:^(id result) {
            
        }];
        buttonVC.itemName = model.category;
        [self.navigationController pushViewController:buttonVC animated:YES];
    }
    else if (button.tag == 4) {
        ZGLPlayHotButtonVC *buttonVC = [[ZGLPlayHotButtonVC alloc] init];
        buttonVC.cityName = str;
        NSMutableString *urlStr = [NSMutableString stringWithString: @"http://wl.myzaker.com/?_appid=AndroidPhone&_v=6.4.1&_version=6.41&c=movie_list&city="];
        [urlStr appendString:str];
        NSString *striEncoding = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        [ZGLHTTPTOOL GETWithURL:striEncoding withBody:nil withHttpHead:nil responseStyle:JSON withSuccess:^(id result) {
            self.nextModelArr = [NSMutableArray arrayWithCapacity:0];
            NSDictionary *dic = [result objectForKey:@"data"];
            NSArray *arr = [dic objectForKey:@"weekends"];
            for (NSDictionary *dic in arr) {
                ZGLPlayDetailModel *model = [ZGLPlayDetailModel modWithDic:dic];
                [self.nextModelArr addObject:model];
                buttonVC.modelArr = self.nextModelArr;
            }
        } withFail:^(id result) {
            
        }];
        buttonVC.itemName = model.category;
        [self.navigationController pushViewController:buttonVC animated:YES];
    }
    else if(button.tag == 5) {
        ZGLPlayHotButtonVC *buttonVC = [[ZGLPlayHotButtonVC alloc] init];
        buttonVC.cityName = str;
        NSMutableString *urlStr = [NSMutableString stringWithString: @"http://wl.myzaker.com/?_appid=AndroidPhone&_v=6.4.1&_version=6.41&c=activity_list&category=9&city="];
        [urlStr appendString:str];
        [urlStr appendString:@"&p=0&size=20"];
        NSString *striEncoding = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        [ZGLHTTPTOOL GETWithURL:striEncoding withBody:nil withHttpHead:nil responseStyle:JSON withSuccess:^(id result) {
            self.nextModelArr = [NSMutableArray arrayWithCapacity:0];
            NSDictionary *dic = [result objectForKey:@"data"];
            NSArray *arr = [dic objectForKey:@"weekends"];
            for (NSDictionary *dic in arr) {
                ZGLPlayDetailModel *model = [ZGLPlayDetailModel modWithDic:dic];
                [self.nextModelArr addObject:model];
                buttonVC.modelArr = self.nextModelArr;
            }
        } withFail:^(id result) {
            
        }];
        buttonVC.itemName = model.category;
        [self.navigationController pushViewController:buttonVC animated:YES];
        
    }else {
        ZGLPlayHotButtonVC *buttonVC = [[ZGLPlayHotButtonVC alloc] init];
        buttonVC.cityName = str;
        NSMutableString *urlStr = [NSMutableString stringWithString: @"http://wl.myzaker.com/?_appid=AndroidPhone&_v=6.4.1&_version=6.41&c=activity_list&category=3&city="];
        [urlStr appendString:str];
        [urlStr appendString:@"&p=0&size=20"];
        NSString *striEncoding = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        [ZGLHTTPTOOL GETWithURL:striEncoding withBody:nil withHttpHead:nil responseStyle:JSON withSuccess:^(id result) {
            self.nextModelArr = [NSMutableArray arrayWithCapacity:0];
            NSDictionary *dic = [result objectForKey:@"data"];
            NSArray *arr = [dic objectForKey:@"weekends"];
            for (NSDictionary *dic in arr) {
                ZGLPlayDetailModel *model = [ZGLPlayDetailModel modWithDic:dic];
                [self.nextModelArr addObject:model];
                buttonVC.modelArr = self.nextModelArr;
            }
        } withFail:^(id result) {
            
        }];
        buttonVC.itemName = model.category;
        [self.navigationController pushViewController:buttonVC animated:YES];
    }
   
}
#pragma mark 5 ** 六个button数据处理
-(void)buttonHandleData:(NSString *)cityName {
    NSMutableString *urlStr = [NSMutableString stringWithString: @"http://wl.myzaker.com/?_appid=AndroidPhone&_v=6.4.1&_version=6.41&c=category_list&city="];
    [urlStr appendString:cityName];
    NSString *striEncoding = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [ZGLHTTPTOOL GETWithURL:striEncoding withBody:nil withHttpHead:nil responseStyle:JSON withSuccess:^(id result) {
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
        self.cityModelArr = [NSMutableArray arrayWithCapacity:0];
        NSDictionary *dic = [result objectForKey:@"data"];
        NSArray *arr = [dic objectForKey:@"list"];
        for (NSDictionary *dic in arr) {
            ZGLPlayFirstModel *model = [ZGLPlayFirstModel modWithDic:dic];
            [array addObject:model];
        }
        self.cityModelArr = array;
        [self creatButtonView];
    } withFail:^(id result) {
        
    }];
    
    
}
#pragma mark 6 - 创建定位button
-(void)creatLoactionButton {
    self.button = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.view addSubview:self.button];
    self.button.frame = CGRectMake(40 * ScaleX, 100 * ScaleX, 295 * ScaleX, 40 * ScaleX);
    self.button.layer.cornerRadius = 10;
    self.button.layer.masksToBounds = YES;
    self.button.layer.borderWidth = 0.4;
    self.button.layer.borderColor = [UIColor lightGrayColor].CGColor;
    [self.button addTarget:self action:@selector(chooseCity:) forControlEvents:UIControlEventTouchUpInside];
    [self.button setTintColor:[UIColor blackColor]];
}
#pragma mark 6 ** 定位button点击事件
-(void)chooseCity:(UIButton *)button {
    ZGLCityListVC *cityListVC = [[ZGLCityListVC alloc] init];
    cityListVC.delegate = self;
    NSString *str = [self.button.titleLabel.text substringFromIndex:3];
    cityListVC.currentCity = str;
    [self.navigationController pushViewController:cityListVC animated:YES];
}
#pragma mark 7 - ZGLCityListVC协议方法实现
-(void)passCityName:(NSString *)cityName {
    for (UIView *view in self.view.subviews) {
        if ([view isKindOfClass:[ZGLPlayFirstView class]]) {
            [view removeFromSuperview];
        }
    }
    if ([self.cityArr containsObject:cityName]) {
        [self buttonHandleData:cityName];
        self.button.frame = CGRectMake(40 * ScaleX, 300 * ScaleX, 295 * ScaleX, 40 * ScaleX);
    }else {
       self.button.frame = CGRectMake(40 * ScaleX, 100 * ScaleX, 295 * ScaleX, 40 * ScaleX);
    }
    [self creatMapViewWithCityName:cityName];
    [self.button setTitle:[NSString stringWithFormat:@"定位：%@", cityName] forState:UIControlStateNormal];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
